#!./perl


# ------------------------------------------------------------

# This script was baseed on the generic_mailer2.pl, by phil hooper (pjh@netcom.com)
# It was modified for the query engine behind the Catalyst Interlink Solaris Product Expo
# Dec 14, 1994 Marc Sacoolas

sub get_request {

    # Subroutine get_request reads the POST or GET form request from STDIN
    # into the variable  $request, and then splits it into its
    # name=value pairs in the associative array %rqpairs.
    # The number of bytes is given in the environment variable
    # CONTENT_LENGTH which is automatically set by the request generator.

    # Encoded HEX values and spaces are decoded in the values at this
    # stage.

    # $request will contain the RAW request. N.B. spaces and other
    # special characters are not handler in the name field.

    if ($ENV{'REQUEST_METHOD'} eq "POST") {
	read(STDIN, $request, $ENV{'CONTENT_LENGTH'});
    } elsif ($ENV{'REQUEST_METHOD'} eq "GET" ) {
	$request = $ENV{'QUERY_STRING'};
    }


    @names = &url_decode(split(/[&=]/, $request));
    %rqpairs = @names;

}

sub url_decode {

#	Decode a URL encoded string or array of strings 
#		+ -> space
#		%xx -> character xx


    foreach (@_) {
	tr/+/ /;
	s/%(..)/pack("c",hex($1))/ge;
    }
    @_;
}

sub html_header {

    # Subroutine html_header sends to Standard Output the necessary
    # material to form an HHTML header for the document to be
    # returned, the single argument is the TITLE field.

    local($title) = @_;

    print "Content-type: text/html\n\n";
    print "<html><head>\n";
    print "<title>$title</title>\n";
    print "</head>\n<body>\n";
}

sub html_trailer {

    # subroutine html_trailer sends the trailing material to the HTML
    # on STDOUT.

    local($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst)
	= gmtime;

    local($mname) = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
		     "Aug", "Sep", "Oct", "Nov", "Dec")[$mon];
    local($dname) = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri",
		     "Sat")[$wday]; 

    print "<p>\nGenerated by: <var>$0</var><br>\n";
    print "Date: $hour:$min:$sec UT on $dname $mday $mname $year.<p>\n";
    print "</body></html>\n";
}

# --------- Everything above here is generic ---------

# Define fairly-constants


#
# Get the input, output header
#

&get_request;


#
# make sure nobody tries to execute a subshell
#

$rqpairs{'mailto'} =~ s/~!/ ~!/g;

#
# check for REQUIRED keyword.  Set flag if value is required
# but not provided, then put up a page and forget about sending
# query.  The REQUIRED keyword was still used to insure a
# key word entry. 
#

@check_reqs = @names;
for $i (0..$#check_reqs){
    $name = shift(@check_reqs);
    $value = shift(@check_reqs);

    if ($name =~ /REQUIRED/) {
        if ($value eq "") {
            $bad = $name;
            $bad =~ s/\s*REQUIRED\s*//;
	    push(@missing, $bad);
	}
    }
}


if ($#missing >= 0) {
    &html_header('Catalyst CDware');
    print "<HR>\n";
    print "<H3>Please provide a key word for your query.</H3>\n";
    print "<HR>\n";
    print "<H3>Go back and try again</H3>\n";

    &html_trailer;
    exit 0;
}

#
# place keyword in the environment in hopes that it can be inherited
# by it's child csh
#
$keyword = $rqpairs{'KEYWORD'} ;
$install = $rqpairs{'install'} ;
$testdrive = $rqpairs{'testdrive'} ;
$ostype = $rqpairs{'ostype'} ;
#
# see if "ALL" or "" with no filters was selected
#
if ( $keyword eq "ALL" && $ostype eq "OS Type" && $install eq "" && $testdrive eq "" ) {
	print "Location: file:///tmp/httpd/.products/.categories/companies.html\n\n";
	exit 0;
}
#
# settle for file transfer for now, 12-14-94, change later
#
open (QUERY,">/tmp/httpd/tmp");
print QUERY "$keyword^";
print QUERY "$install^";
print QUERY "$testdrive^";
print QUERY "$ostype^";
close (QUERY) ;


#
# launch query results page generater
#
open (SORT,"|./catIPX.csh /tmp/perl.log") ;
close (SORT) ;

#
# if only one, csh will deposite file to go to
#
if ( -e "/tmp/httpd/only_one" ) {

open (ONLY,"/tmp/httpd/top");
while (<ONLY>) {
        $line = $_;
        @fields = split(/\^/,$_);
}
close (OPEN);
unlink ("/tmp/httpd/only_one");

#print "Location: http://localhost:7999/@fields[2]\n\n";
print "Location: file:///tmp/httpd/.products/@fields[2]/index.html\n\n";
} 
#
# display page
#
print "Location: file:///tmp/httpd/tmppage.html\n\n";

